package main

import (
	"encoding/json"
	"fmt"
	"time"

	"gitee.com/knowgo/ego/core"
	"gitee.com/knowgo/ego/core/data"
	"gitee.com/knowgo/ego/egoc"
	"gitee.com/knowgo/ego/log"
)

type Loader struct {
	Acc *Access `ego:"optional"`
}

type A struct {
	B        string       `ego:"abc, required ,'','123',[1:10],(5,6,'123',y'11)"`
	C        int          `json:"c,omitempty"`
	Children []A          `json:"children,omitempty"`
	Map      map[string]A `json:"map,omitempty"`
	Date     time.Time    `json:"date" ego:"date"`
}

func (*Loader) Group() egoc.Group {
	return egoc.Group{
		Name: "a",
	}
}

func (l *Loader) TestA(b *int) (A, error) {
	fmt.Println("------- TestA -------")
	fmt.Println(*b)
	fmt.Println(l.Acc)
	return A{
		B: fmt.Sprintf("%d", *b),
	}, nil
}

func (Loader) TestB(a A) (int, error) {
	fmt.Println("------- TestB -------")
	fmt.Println(a)
	return 0, nil
}

func (Loader) TestMap(a map[string]A) (int, error) {
	fmt.Println("------- TestMap -------")
	fmt.Println(a)
	return 0, nil
}

func (Loader) TestArray(a []A) (int, error) {
	fmt.Println("------- TestArray -------")
	fmt.Println(a)
	return 0, nil
}

type Access struct {
	U string
}

func main() {
	g := log.NewLog()
	c := core.NewCore(g)
	c.RegisterInjector(func(ctx *egoc.Context) (*Access, error) {
		return &Access{U: "inject-100"}, nil
		// return nil, errors.New("11")
	})
	c.Register(&Loader{})

	var TestB = &data.ServiceRequest{
		Service: "a.TestB",
		Argument: &A{B: "1234567890",
			Map: map[string]A{
				"aa": {B: "100"},
				"ab": {B: "101"},
			},
			Children: []A{
				{B: "A00"},
				{B: "A01"},
				{B: "A0001"},
			},
		},
	}
	// var TestA = &data.ServiceRequest{
	// 	Service:  "a.TestA",
	// 	Argument: 10,
	// }
	// var TestMap = &data.ServiceRequest{
	// 	Service: "a.TestMap",
	// 	Argument: map[string]A{
	// 		"aa": {B: "100"},
	// 		"ab": {B: "101"},
	// 	},
	// }
	// var TestArray = &data.ServiceRequest{
	// 	Service: "a.TestArray",
	// 	Argument: []A{
	// 		{B: "100"},
	// 		{B: "101"},
	// 	},
	// }

	fmt.Println(time.Now().UnixMilli())
	var reqs = data.Request{TestB} // , TestA, TestMap, TestArray}

	reqData, _ := json.Marshal(reqs)

	fmt.Println(string(reqData))

	var requests data.Request
	// json.Unmarshal([]byte(`[{"service":"a.TestB","alias":"","args":{"B":"1234567890","children":[{"B":"A00","date":"2003-01-01 00:01:02"},{"B":"A01","date":"2022-01-01 00:00:01"},{"B":"A0001","date":"2021-01-09 00:10:00"}],"map":{"aa":{"B":"100","date":"2001-01-01 00:00:01"},"ab":{"B":"101","date":"0001-01-01 00:00:00"}},"date":"0001-01-01 00:00:00"}}]`), &requests)
	// json.Unmarshal([]byte(`[{"service":"a.TestB","alias":"","args":{"B":"1234567890","children":[{"B":"A00","date":"1661752369366"},{"B":"A01","date":"1661752369366"},{"B":"A0001","date":"1661752369366"}],"map":{"aa":{"B":"100","date":"1661752369366"},"ab":{"B":"101","date":"1661752369366"}},"date":"1661752369366"}}]`), &requests)
	json.Unmarshal([]byte(`[{"service":"a.TestB","alias":"","args":{"B":"1234567890","children":[{"B":"A00","date":"2003-01-01"},{"B":"A01","date":"2022-01-01"},{"B":"A0001","date":"2021-01-09"}],"map":{"aa":{"B":"100","date":"2001-01-01"},"ab":{"B":"101","date":"2022-01-01"}},"date":"2012-01-01"}}]`), &requests)
	// json.Unmarshal([]byte(`[{"service":"a.TestB","alias":"","args":{"B":"1234567890","children":[{"B":"A00","date":"0001-01-01 00:00:00"},{"B":"A01","date":"0001-01-01 00:00:00"},{"B":"A0001","date":"0001-01-01 00:00:00"}],"map":{"aa":{"B":"100","date":"0001-01-01 00:00:00"},"ab":{"B":"101","date":"0001-01-01 00:00:00"}},"date":"0001-01-01 00:00:00"}}]`), &requests)

	res, _, _, err := c.Service(requests, &egoc.Context{})
	if err != nil {
		panic(err)
	}

	fmt.Println("------------ response -----------")
	fmt.Println(res)

	// var injectValueMap = map[reflect.Type]*arguments.InjectValue{}

	// c.CreateService(injectValueMap)

	// fmt.Println(c.ReponseTypes)
	// tp := reflect.TypeOf(A{})
	// cout := tp.NumField()
	// for n := 0; n < cout; n++ {
	// 	fd := tp.Field(n)
	// 	if fd.IsExported() {
	// 		core.ParseTag(&fd, func(tagItem string) {
	// 			fmt.Println("error", tagItem)
	// 		})
	// 	}
	// }
}
